package com.example.robertluozizhao.framecollectdemo.activity;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.ImageView;

import com.example.robertluozizhao.framecollectdemo.R;
import com.example.robertluozizhao.framecollectdemo.adapter.TabPagerAdapter;
import com.example.robertluozizhao.framecollectdemo.fragment.FragmetManager_WritePadView01;
import com.example.robertluozizhao.framecollectdemo.fragment.TextFragmetManager02;
import com.example.robertluozizhao.framecollectdemo.fragment.TextFragmetManager03;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author luo_zi_zhao
 * @version V1.0 当前版本描述信息
 * @FileName com.example.robertluozizhao.framecollectdemo.activity
 * @date 2019/4/25 17:03
 */

public class FragmentManagerActivity extends AppCompatActivity {


    @BindView(R.id.vp_sx_wp)
    ViewPager vpSxWp;
    @BindView(R.id.tl_sx_wp)
    TabLayout tlSxWp;
    private TabPagerAdapter tabAdapter;
    private List<Fragment> fragmentList;
    private String tabTitles[] = new String[]{"TextFragmetManager01", "TextFragmetManager02", "TextFragmetManager03"};


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_manager);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new FragmetManager_WritePadView01());
        fragmentList.add(new TextFragmetManager02());
        fragmentList.add(new TextFragmetManager03());


        tabAdapter = new TabPagerAdapter(getSupportFragmentManager(), this, fragmentList, tabTitles);
        vpSxWp.setAdapter(tabAdapter);
        tlSxWp.setupWithViewPager(vpSxWp);


        for (int i = 0; i < fragmentList.size(); i++) {
            TabLayout.Tab tab = tlSxWp.getTabAt(i);
            //注意！！！这里就是添加我们自定义的布局
            tab.setCustomView(tabAdapter.getCustomView(i));
            //这里是初始化时，默认item0被选中，setSelected（true）是为了给图片和文字设置选中效果，代码在文章最后贴出
            if (i == 0) {
                (tab.getCustomView().findViewById(R.id.tab_item_iv)).setSelected(true);
                (tab.getCustomView().findViewById(R.id.tab_item_tv)).setSelected(true);
                setColorTint(((ImageView) (tab.getCustomView().findViewById(R.id.tab_item_iv))), true);
            }
        }

        tlSxWp.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                (tab.getCustomView().findViewById(R.id.tab_item_iv)).setSelected(true);
                (tab.getCustomView().findViewById(R.id.tab_item_tv)).setSelected(true);
                setColorTint(((ImageView) (tab.getCustomView().findViewById(R.id.tab_item_iv))), true);
                vpSxWp.setCurrentItem(tab.getPosition());
                int position = tab.getPosition();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                (tab.getCustomView().findViewById(R.id.tab_item_iv)).setSelected(false);
                (tab.getCustomView().findViewById(R.id.tab_item_tv)).setSelected(false);
                setColorTint(((ImageView) (tab.getCustomView().findViewById(R.id.tab_item_iv))), false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


    private void setColorTint(ImageView imageView, boolean selected) {
        Drawable originalDrawable = imageView.getDrawable();
        Drawable tintDrawable = DrawableCompat.wrap(originalDrawable).mutate();
        if (selected) {
            DrawableCompat.setTint(tintDrawable, Color.parseColor("#FFFFFF"));
        } else {
            DrawableCompat.setTint(tintDrawable, Color.parseColor("#ffd675"));
        }
        imageView.setImageDrawable(tintDrawable);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
